<?php

/**
 * Created by PhpStorm.
 * User: gongxiaohua
 * Date: 17年2月22日
 * Time: 上午9:50
 */
namespace Warehouse\Model;

use Warehouse\Model\RecieveInvoicesModel;
use Inbound\Service\PrepareneedsService;

class RecieveDetailsModel extends RecieveInvoicesModel
{
    //数据库对象
    public $Recieve_details = null;
    //收货明细表
    static $table = 'recieve_details';
    //数据
    public $Recieve_details_data = null;
    //收货明细表ID
    public $Recieve_details_id;

    /**
     * @param string $table
     * @param array  $param
     */
    public function __construct($table = '', $param = array())
    {
        //print_r($this->_db);exit;
        $this->Recieve_details = M(self::$table, 'wms_', $this->_db);
        parent::__construct();
    }

    /**
     * @param array $_array
     *
     * @return mixed
     * 过滤查询条件
     */
    public function get_recieve_details_model(&$_array = array(), $flag = true)
    {
        if ($flag) {
            if ($_array) {
                $_array = array_filter($_array);
            } else {
                //$_array = array('status' => 10);
            }
            $_array = PrepareneedsService::trim_array($_array);
            foreach ($_array as $key => $_arr) {
                switch ($key) {
                    case 'claim_arrive_time_from':
                        $_array['claim_arrive_time'][] = array(
                            'egt',
                            $_arr
                        );
                        unset($_array['claim_arrive_time_from']);
                        break;
                    case 'claim_arrive_time_to':
                        $_array['claim_arrive_time'][] = array(
                            'elt',
                            $_arr
                        );
                        unset($_array['claim_arrive_time_to']);
                        break;
                }
            }
            return $this->Recieve_details->where($_array);
        } else {
            return $this->Recieve_details->where($_array);
        }
    }

    /**
     * @param array $_array
     * @param null  $model
     *
     * @return mixed
     * 插入收货明细表
     */
    public function add_recieve_details_table($_array = array(), &$model = null)
    {
        $this->Recieve_details_id = $model
            ->table($this->_db.'.wms_'.self::$table)
            ->add($_array);
        return $this->Recieve_details_id;
    }

    /**
     *更新操作
     */
    public function update_recieve_details($_array = array(), $data = array(), &$model = null)
    {
        $model = $model == null ? $this->Recieve_details : $model->table($this->_db.".wms_".self::$table);
        $res   = $model->where($_array)->save($data);
        return $res;
    }

    /**
     * 根据id获取收货明细单
     *
     * @param $id
     *
     * @return mixed
     */
    public function get_recieve_details_by_id(&$id)
    {
        $where  = array('wms_recieve_details.id' => $id);
        $fields = 'wms_recieve_details.id, wms_recieve_details.sku, wms_recieve_details.sku_name, 
            wms_attr_log.user_id, wms_attr_log.att_time, wms_recieve_details.arrival_quantity,
            wms_recieve_invoices.purchaseorder_id, wms_recieve_invoices.supplier,wms_recieve_invoices.transfer_hopper_id,
            wms_recieve_details.logisticsCheck,wms_purchaseorders.remark,wms_attr_log.logic_v,skusystem_sku_logic.logic_attr,
            wms_recieve_details.site_id';

        

        return $this->Recieve_details->join(
            'wms_recieve_invoices ON wms_recieve_details.recieve_invoice_id=wms_recieve_invoices.id', 'LEFT'
        )
            ->join('wms_purchaseorders ON wms_purchaseorders.id=wms_recieve_invoices.purchaseorder_id', 'LEFT')
            ->join('wms_attr_log ON wms_attr_log.recieve_detail_id=wms_recieve_details.id', 'LEFT')
            ->join('skusystem_sku_logic ON skusystem_sku_logic.sku=wms_recieve_details.sku', 'LEFT')
            ->where($where)
            ->order('id DESC')
            ->field($fields)
            ->find();
    }

    /**
     * 修改收货明细表属性确认状态
     *
     * @param $sku
     */
    public function updateLogisticsCheck($sku)
    {
        $data['logisticsCheck'] = 20;
        $where['sku']            = $sku;
        $this->Recieve_details->where($where)
            ->save($data);
    }

    /**
     * 根据条件获取sku质检标准确认信息
     *
     * @param $_array
     *
     * @return mixed
     */
    public function getCheckStandardDetail(&$_array)
    {
        $model = $this->Recieve_details;

        $where_map = array();
        foreach ($_array as $key => $value) {
            switch ($key) {
                case 'batchCode':
                    $where_map['wms_recieve_details.batch_code'] = $value;
                    break;
                case 'recieveDetailsId':
                    $where_map['wms_recieve_details.id'] = $value;
                    break;
                case 'skuCheckSign':
                    if ('-1' == $value) {
                        $where_map['wms_recieve_details.skuchecksign'] = 0;
                    } else {
                        $where_map['wms_recieve_details.skuchecksign'] = $value;
                    }
                    break;
            }
        }

        $fiels = 'wms_recieve_details.id, wms_recieve_details.batch_code, wms_recieve_details.sku, wms_recieve_details.sku_name,
            wms_recieve_invoices.recieve_man, wms_recieve_invoices.purchaseorder_id, wms_recieve_invoices.export_tax_rebate,
            skusystem_qc_standard.standard, wms_purchaseorders.purchase_id, wms_recieve_details.skuchecksign,wms_recieve_details.site_id';

        $result = $model->join(
            'wms_recieve_invoices ON wms_recieve_invoices.id=wms_recieve_details.recieve_invoice_id', 'LEFT'
        )
            ->join('wms_purchaseorders ON wms_purchaseorders.id=wms_recieve_invoices.purchaseorder_id', 'LEFT')
            ->join('skusystem_sku_qc_standard ON skusystem_sku_qc_standard.sku=wms_recieve_details.sku', 'LEFT')
            ->join(
                'skusystem_qc_standard ON skusystem_qc_standard.id=skusystem_sku_qc_standard.qc_standard_id', 'LEFT'
            )
            ->where($where_map)
            ->field($fiels)
            ->select();

        return $result;
    }

    /**
     * 质量标准确认
     *
     * @param $_array
     */
    public function checkSkuStandard(&$_array)
    {
        $model                = $this->Recieve_details;
        $map                  = array(
            'id'           => array(
                'IN',
                $_array
            ),
            'skuchecksign' => 10
        );
        $data['skuchecksign'] = 20;
        $data['check_man']    = $_SESSION['current_account']['id'];
        $data['check_time']   = date('Y-m-d H:i:s', time());

        return $model->where($map)
            ->save($data);
    }

    /**
     * 获取扫描质检详情
     *
     * @param $_array
     */
    public function getScanCheckDetail(&$_array)
    {
        $model = $this->Recieve_details;

        $where_map = array();
        foreach ($_array as $key => $value) {
            switch ($key) {
                case 'batchCode':
                    $where_map['wms_recieve_details.batch_code'] = $value;
                    break;
                case 'recieveDetailsId':
                    $where_map['wms_recieve_details.id'] = $value;
                    break;
                case 'skuCheckSign':
                    if ('-1' == $value) {
                        $where_map['wms_recieve_details.skuchecksign'] = 0;
                    } else {
                        $where_map['wms_recieve_details.skuchecksign'] = $value;
                    }
                    break;
            }
        }

        $fiels = 'wms_recieve_details.id, wms_recieve_details.batch_code, wms_recieve_details.sku, wms_recieve_details.sku_name,
            wms_recieve_invoices.purchaseorder_id, wms_recieve_invoices.export_tax_rebate,wms_recieve_invoices.supplier,
            wms_recieve_invoices.transfer_hopper_id, wms_purchaseorders.remark, skusystem_sku_logic.logic_attr,
            wms_recieve_details.skuchecksign, wms_recieve_details.arrival_quantity, wms_recieve_details.check_count,
            wms_recieve_details.logisticsCheck,wms_check_quality_details.qualified_quantity, wms_check_quality_details.unquality_count,
            wms_check_quality_details.remark AS unquality_remark,wms_purchaseorder_details.quantity,wms_recieve_details.site_id';

        $result = $model->join(
            'wms_recieve_invoices ON wms_recieve_invoices.id=wms_recieve_details.recieve_invoice_id', 'LEFT'
        )
            ->join('wms_purchaseorders ON wms_purchaseorders.id=wms_recieve_invoices.purchaseorder_id', 'LEFT')
            ->join(
                'wms_purchaseorder_details ON wms_purchaseorders.id=wms_purchaseorder_details.purchaseorder_id 
            AND wms_purchaseorder_details.sku=wms_recieve_details.sku', 'LEFT'
            )
            ->join('skusystem_sku_logic ON skusystem_sku_logic.sku=wms_recieve_details.sku', 'LEFT')
            ->join(
                'wms_check_quality_details ON wms_check_quality_details.recieve_detail_id=wms_recieve_details.id
                 AND wms_check_quality_details.site_id = wms_recieve_details.site_id',
                'LEFT'
            )
            ->where($where_map)
            ->field($fiels)
            ->order('wms_recieve_details.id asc')
            ->select();

        return $result;
    }

    /**
     * 根据收货明细单ids获取扫描质检数据保存需要的详细信息
     *
     * @param $_array
     *
     * @return mixed
     */
    public function getScanCheckInfoById(&$_array)
    {
        $model = $this->Recieve_details;

        $where_map = array(
            'wms_recieve_details.id' => array(
                'IN',
                $_array
            )
        );

        $fiels = 'wms_recieve_details.id AS recieve_detail_id, wms_recieve_details.batch_code, wms_recieve_details.sku,
            wms_recieve_details.sku_name,wms_recieve_invoices.purchaseorder_id, wms_recieve_invoices.export_tax_rebate,
            wms_recieve_invoices.supplier AS supplier_id, wms_recieve_invoices.warehouseid, wms_recieve_details.sku_standard,
            wms_recieve_invoices.purchase_id,wms_recieve_details.site_id,wms_recieve_details.storage_position, 
            wms_purchaseorders.store, wms_purchaseorders.enterprise_dominant, wms_purchaseorders.transfer_hopper_id,
            wms_purchaseorders.transfer_type,wms_recieve_details.skuchecksign, wms_recieve_details.arrival_quantity,
             wms_recieve_details.check_count';

        $result = $model->join(
            'wms_recieve_invoices ON wms_recieve_invoices.id=wms_recieve_details.recieve_invoice_id', 'LEFT'
        )
            ->join('wms_purchaseorders ON wms_purchaseorders.id=wms_recieve_invoices.purchaseorder_id', 'LEFT')
            ->where($where_map)
            ->field($fiels)
            ->select();

        return $result;
    }

}